define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'customer.package.user/index' + location.search,
                    add_url: 'customer.package.user/add/package_id/' + Config['package_id'] + '/identity/' + Config['identity'],
                    // edit_url: 'customer.package.user/edit/package_id/'+Config['package_id']+'/identity/'+Config['identity'],
                    del_url: 'customer.package.user/del',
                    multi_url: 'customer.package.user/multi',
                    import_url: 'customer.package.user/import',
                    table: 'customer_package_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'package_user_id',
                sortName: 'package_user_id',

                queryParams: (params) => {
                    var filter = JSON.parse(params.filter)
                    var op = JSON.parse(params.op)

                    filter.package_id = Config['package_id']
                    op.package_id = '='

                    filter.identity = Config['identity']
                    op.identity = '='

                    params.filter = JSON.stringify(filter)
                    params.op = JSON.stringify(op)

                    return params
                },

                columns: [
                    [
                        {checkbox: true},
                        {field: 'package_user_id', title: __('ID')},
                        {field: 'admin.nickname', title: Config['identity'] == 0 ? '会计名称' : '外勤名称'},
                        {
                            field: 'admin.status',
                            title: __("当前状态"),
                            searchList: {"normal": __('在职'), "hidden": __('已离职')},
                            formatter: Table.api.formatter.status
                        },
                        // {field: 'package_id', title: __('Package_id')},
                        // {field: 'identity', title: __('Identity')},
                        {
                            field: 'create_time',
                            title: __('Create_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
